<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 幻灯片控制器
 * +----------------------------------------------------------------------
 */
namespace app\index\controller\admin;

use app\index\model\IndexSlider;
use app\index\validate\Slider as SliderValidate;

use think\facade\View;
use think\facade\Config;
use think\facade\Request;

class Slider extends Base
{
  public function index() {
    $slider = IndexSlider::order('id desc')->paginate(Config::get('basic.page_size'));
    View::assign('slider', $slider);
    return View::fetch();
  }

  public function sliderAdd() {
    $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    if ($id > 0) {
      //是修改操作
      if(Request::isPost()) {
        (new SliderValidate())->goCheck();
        $data = Request::post();
        $result = IndexSlider::update($data, ['id' =>  $id]);
        if ($result) {
          adminLog(); //写入日志
          return success('修改成功', 'index');
        }
        else {
          return error('修改失败');
        }
      } else {
        $info = IndexSlider::where('id', '=', $id)->find();
        View::assign('info', $info);
        return View::fetch();
      }
    }
    else {
      // 新增操作
      if (Request::isPost()) {
        $data = Request::post();
        //验证
        (new SliderValidate())->goCheck();
        //添加
        $result = IndexSlider::create($data);
        if ($result) {
          adminLog(); //写入日志
          return success('添加成功', 'index');
        }
        else {
          return error('添加失败');
        }
      } else {
        return View::fetch();
      }
    }
  }

  public function delete() {
    $result = $this->setStatus('index_slider', 'delete');
    if ($result['error'] == 1) {
      return error($result['msg']);
    }
    else {
      adminLog(); //写入日志
      return success($result['msg']);
    }
  }

  public function sliderState() {
    $result = $this->setStatus('index_slider', 'status');
    if ($result['error'] == 1) {
      return error($result['msg']);
    }
    else {
      adminLog(); //写入日志
      return success($result['msg']);
    }
  }

}

?>